package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddbookList implements IOperation{

    @Override
    public void  work(BookList bookList) {
        System.out.println("新增图书");
        //        判断书架满了吗
        int currentSize = bookList.getUseSize();
        if (currentSize == bookList.getBooks().length){
            System.out.println("书架已经满了奥");
            return;
        }

//        构建对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = sc.nextLine();

        System.out.println("请输入作者：");
        String author = sc.nextLine();

        System.out.println("请输入类型：");
        String type = sc.nextLine();

        System.out.println("请输入价格：");
        int price = sc.nextInt();



        Book newBook = new Book(name,author,price,type);


//        判断书架有咩有这本书

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)){
                System.out.println("已经有这本书，不能再次添加");
                System.out.println(book);
                return;
            }
        }

//        增添这本书

        bookList.setBooks(currentSize,newBook);

        bookList.setUseSize(currentSize+1);
        System.out.println("新增图书成功");

    }
}
